package com.cyms.modules.order.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.Query;
import com.cyms.modules.base.dao.CarDao;
import com.cyms.modules.order.dao.OrderCarDao;
import com.cyms.modules.order.entity.OrderCarEntity;
import com.cyms.modules.order.service.OrderCarService;
import com.cyms.modules.sys.entity.SysUserEntity;


@Service("orderCarService")
public class OrderCarServiceImpl extends ServiceImpl<OrderCarDao, OrderCarEntity> implements OrderCarService {

	@Autowired
	private OrderCarDao orderCarDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<OrderCarEntity> page = this.selectPage(
                new Query<OrderCarEntity>(params).getPage(),
                new EntityWrapper<OrderCarEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public List<Map<String, Object>> queryAmountQueryPage(Map<String, Object> params) {
		List<Map<String, Object>> list = orderCarDao.selectAmountQueryPage(params);
		return list;
	}

	@Override
	public List<Map<String, Object>> queryPayQueryPage(Map<String, Object> params) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = orderCarDao.selectPayQueryPage(params);
		return list;
	}

	@Override
	public List queryCarCostQueryPage(Map<String, Object> params) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = orderCarDao.selectCarCostQueryPage(params);
		return list;
	}

	@Override
	public List queryCarCostDetail(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return orderCarDao.queryCarCostDetail(params);
	}

	@Override
	public List queryCarAmountDetail(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return orderCarDao.queryCarAmountDetail(params);
	}

	@Override
	public List queryCarPayDetail(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return orderCarDao.queryCarPayDetail(params);
	}

	@Override
	public void batchFinishOrder(SysUserEntity user, String[] codes, Date startDate, Date endDate) {
		// TODO Auto-generated method stub
		List<OrderCarEntity> orderCarList = this.selectList(new EntityWrapper<OrderCarEntity>()
				.in(!Arrays.asList(codes).isEmpty(), "car_code", Arrays.asList(codes))
				.ge(startDate != null, "start_date", startDate)
				.le(endDate != null, "end_date", endDate));
		orderCarList.parallelStream().forEach(oc->{
			oc.setAlreadyPay(oc.getTotalPay());
			this.updateById(oc);
		});
	}
}
